package com.sczmgk.hotline_pzh.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.sczmgk.hotline_pzh.R;

/**
 * Created by Administrator on 2016/11/23 0023.
 */

public class MyNewsListView extends ListView {

    /*资讯模块顶部头的标题*/
    private TextView mTvMainNewsTitle;
    /*资讯模块顶部头的图标*/
    private ImageView mIvMainNewsIcon;
    /*资讯模块顶部头的时间*/
    private TextView mTvMainNewsDate;
    /*资讯模块顶部头的更多按钮*/
    private TextView mTvMainNewsLoadMore;

    /*更多按钮的控件*/
    private LinearLayout mLayoutLoadMore;

    /*顶部头的条目控件*/
    private LinearLayout mLayoutHeader;

    private BitmapUtils bitmapUtils;
    private float StartX;
    private float StartY;
    private int flag = 0;

    public MyNewsListView(Context context) {
        this(context , null);
    }

    public MyNewsListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        addHeader(context , attrs);
    }

    private void addHeader(Context context, AttributeSet attrs) {

        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_news_header, null);

        //模块头的标题
        mTvMainNewsTitle = (TextView) view.findViewById(R.id.tv_main_news_title); //最新资讯的标题
        //头的图标
        mIvMainNewsIcon = (ImageView) view.findViewById(R.id.iv_main_news_icon); //最新资讯的图标
        //头的更多按钮
        mTvMainNewsLoadMore = (TextView) view.findViewById(R.id.tv_main_news_load_more);  //最新资讯的更多
        //头的时间
        mTvMainNewsDate = (TextView) view.findViewById(R.id.tv_main_news_date);
        //头的更多按钮控件
        mLayoutLoadMore = (LinearLayout) view.findViewById(R.id.ll_main_news_load_more);
        //头
        mLayoutHeader = (LinearLayout) view.findViewById(R.id.ll_main_news_header);

        //添加下划线
//        mTvMainNewsLoadMore.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
//        mTvMainNewsLoadMore.getPaint().setAntiAlias(true);//抗锯齿

        //设置默认图片
        bitmapUtils = new BitmapUtils(getContext());
        bitmapUtils.configDefaultLoadFailedImage(R.drawable.default_center) ;
        bitmapUtils.configDefaultBitmapMaxSize(200,200);

        view.measure(0, 0);
        view.setPadding(0, 0, 0, 0);

        this.addHeaderView(view , null , false);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyNewListView);

        //是否显示更多按钮
        boolean isMoreVisiable = typedArray.getBoolean(R.styleable.MyNewListView_isMoreVisiable, false);
        setLoadMoreVisiable(isMoreVisiable);

        typedArray.recycle();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

    /**
     * 设置头的标题
     */
    public void setTitleText(String titleText) {
        mTvMainNewsTitle.setText(titleText);
    }

    /**
     * 设置头的标题
     */
    public void setTitleText(int res) {
        mTvMainNewsTitle.setText(res);
    }

    /**
     * 获取标题
     */
    public String getTitleText(){
        return mTvMainNewsTitle.getText().toString().trim() ;
    }

    /**
     * 设置头的图标
     */
    public void setNewsIcon(String urlPath) {
        bitmapUtils.display(mIvMainNewsIcon, urlPath);
    }

    /**
     * 设置头的图标
     */
    public void setNewsIcon(int res) {
        mIvMainNewsIcon.setImageResource(res);
    }

    /**
     * 设置头的时间
     */
    public void setNewsDate(String date) {
        mTvMainNewsDate.setText(date);
    }

    /**
     * 设置更多和新闻时间的Visiable
     */
    public void setLoadMoreVisiable(boolean isVisiable){
        mLayoutLoadMore.setVisibility(isVisiable ? View.VISIBLE : View.GONE);
        mTvMainNewsDate.setVisibility(isVisiable ? View.GONE : View.VISIBLE );
        mLayoutHeader.setFocusable(false);
    }

    /**
     * 获取头的更多按钮
     */
    public LinearLayout getNewsLoadMore() {
        return mLayoutLoadMore;
    }

    /**
     * 设置头的更多按钮的点击事件
     */
    public void setOnLoadMoreClickListener(OnClickListener click) {
        mLayoutLoadMore.setOnClickListener(click);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        // 总是调用listview的touch事件处理
        onTouchEvent(ev);

        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            StartX = ev.getX();
            StartY = ev.getY()
            ;
            return false;
        }
        if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            float ScollX = ev.getX() - StartX;
            float ScollY = ev.getY() - StartY; // 判断是横滑还是竖滑，竖滑的话拦截move事件和up事件（不拦截会由于listview和scrollview同时执行滑动卡顿）
            if (Math.abs(ScollX) < Math.abs(ScollY)) {
                flag = 1;
                return true;
            }
            return false;
        }
        if (ev.getAction() == MotionEvent.ACTION_UP) {
            if (flag == 1) {
                return true;
            }
            return false;
        }
        return super.onInterceptTouchEvent(ev);
    }

}
